﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WpfApplication1.Controllers
{
    public class UdpMessageEncryption
    {
        public static string VAR_SEPERATOR = ",";
        public static string LINE_SEPERATOR = "\n";
        public static void decodeUdpMessage(Models.Aircraft aircraft, String udpMessage)
        {
            //determines whether it is "s=" for Sensordata at the beginning or "g=" for gps data
            String[] udpMessageType = Regex.Split(udpMessage, "=");
            switch (udpMessage[0])
            {
                case 's':
                    decodeSensorData(aircraft, udpMessageType[1]);
                    break;
                case 'g':
                    break;
                default:
                    Console.WriteLine("PMR-Error: udpMessage not recognized");
                    break;

            }
        }
        public static void decodeSensorData(Models.Aircraft aircraft, String sensorData)
        {
            IList<String> values = new List<String>();
            foreach (String a in Regex.Split(sensorData, "\n"))
            {
                foreach (String b in Regex.Split(a, ","))
                {
                    values.Add(b);
                }
            }
            if (values.Count < 9)
            {
                Console.WriteLine("PMR-Error: too few sensorData send by flightGear");
            }
            else
            {
                aircraft.aileron = double.Parse(values[0]);
                aircraft.elevator = double.Parse(values[1]);
                aircraft.engine = int.Parse(values[2]);
                aircraft.altitude = int.Parse(values[3]);
                aircraft.speed = int.Parse(values[4]);
                aircraft.rateOfClimb = int.Parse(values[5]);
                aircraft.heading = double.Parse(values[6]);
                aircraft.pitchAngle = double.Parse(values[7]);
                aircraft.bankAngle = double.Parse(values[8]);
                aircraft.outsideAirTemp = int.Parse(values[9]);
                aircraft.groundspeed = double.Parse(values[10]);
                aircraft.rollRate = double.Parse(values[11]);
                aircraft.pitchRate = double.Parse(values[12]);
                aircraft.yawRate = double.Parse(values[13]);
                aircraft.xAcceleration = double.Parse(values[14]);
                aircraft.yAcceleration = double.Parse(values[15]);
                aircraft.zAcceleration = double.Parse(values[16]);
                aircraft.sideSlip = double.Parse(values[17]);
                aircraft.longitude = double.Parse(values[18]);
                aircraft.latitude = double.Parse(values[19]);
                aircraft.track = double.Parse(values[20]);

                //test
                aircraft.desired_longitude = aircraft.longitude;
                aircraft.desired_latitude = aircraft.latitude;

                //aircraft.track = aircraft.heading + aircraft.sideSlip;
            }
        }

        public static string encodeUdpMessage(Models.Aircraft aircraft)
        {
            return aircraft.desired_aileron + VAR_SEPERATOR + aircraft.desired_elevator + VAR_SEPERATOR + aircraft.desired_engine + LINE_SEPERATOR;
        }
    }
}
